#!/usr/bin/env ruby

# ---------------------------------------------------------------------------- #
# Copyright 2002-2023, OpenNebula Project, OpenNebula Systems                  #
#                                                                              #
# Licensed under the Apache License, Version 2.0 (the "License"); you may      #
# not use this file except in compliance with the License. You may obtain      #
# a copy of the License at                                                     #
#                                                                              #
# http://www.apache.org/licenses/LICENSE-2.0                                   #
#                                                                              #
# Unless required by applicable law or agreed to in writing, software          #
# distributed under the License is distributed on an "AS IS" BASIS,            #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.     #
# See the License for the specific language governing permissions and          #
# limitations under the License.                                               #
# ---------------------------------------------------------------------------- #

# clone fe:SOURCE host:remote_system_ds/disk.i vmid dsid
#   - fe is the front-end hostname
#   - SOURCE is the path of the disk image in the form DS_BASE_PATH/disk
#   - host is the target host to deploy the VM
#   - remote_system_ds is the path for the system datastore in the host
#   - vmid is the id of the VM
#   - dsid is the target datastore (0 is the system datastore)

ONE_LOCATION ||= ENV['ONE_LOCATION']

if !ONE_LOCATION
    RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby'
    GEMS_LOCATION     ||= '/usr/share/one/gems'
else
    RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby'
    GEMS_LOCATION     ||= ONE_LOCATION + '/share/gems'
end

# %%RUBYGEMS_SETUP_BEGIN%%
if File.directory?(GEMS_LOCATION)
    real_gems_path = File.realpath(GEMS_LOCATION)
    if !defined?(Gem) || Gem.path != [real_gems_path]
        $LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }

        # Suppress warnings from Rubygems
        # https://github.com/OpenNebula/one/issues/5379
        begin
            verb = $VERBOSE
            $VERBOSE = nil
            require 'rubygems'
            Gem.use_paths(real_gems_path)
        ensure
            $VERBOSE = verb
        end
    end
end
# %%RUBYGEMS_SETUP_END%%

$LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << File.dirname(__FILE__)

require 'vcenter_driver'
CONFIG = VCenterConf.new

src          = ARGV[0]
dst          = ARGV[1]
vm_id        = ARGV[2]
source_ds_id = ARGV[3]

check_valid src, 'src'
check_valid dst, 'dst'
check_valid vm_id, 'vm_id'
check_valid source_ds_id, 'source_ds_id'

target_ds_id = dst.split('/')[-3]
disk_id = dst.split('.')[-1]

src_path_escaped = src.split(':')[-1]
src_path = VCenterDriver::FileHelper.unescape_path(src_path_escaped)

hostname = dst.split(':').first

# Get host ID
host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname)
host_id = host['ID']

# Get datastores refs
source_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore,
                                             source_ds_id)
source_ds_ref = source_ds['TEMPLATE/VCENTER_DS_REF']

target_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore,
                                             target_ds_id)
target_ds_ref = target_ds['TEMPLATE/VCENTER_DS_REF']

check_valid source_ds_ref, 'source_ds'
check_valid target_ds_ref, 'target_ds'

# Get VM info
one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vm_id)

# calculate target path
target_path_escaped = VCenterDriver::FileHelper
                      .get_img_name_from_path(src_path_escaped, vm_id, disk_id)

target_path = VCenterDriver::FileHelper.unescape_path(target_path_escaped)

begin
    op_retry = true

    vi_client = VCenterDriver::VIClient.new_from_host(host_id)
    disk_xpath ="TEMPLATE/DISK[SOURCE=\"#{src_path_escaped}\"]"
    one_disk = one_vm.retrieve_xmlelements(disk_xpath) rescue nil
    one_disk.select! {|e| e['DISK_ID'] == disk_id }
    one_disk = one_disk.first if one_disk

    raise 'Cannot find disk element in vm template' unless one_disk

    vm = VCenterDriver::VirtualMachine.new(vi_client, nil, vm_id)

    if vm.snapshots? || vm.one_snapshots?
        # If there are snapshots we shouldn't retry
        op_retry = false

        raise 'Existing sytem snapshots, cannot change disks. ' \
              'Please remove all snapshots and try again.'
    end

    retries ||= 0

    disk = VCenterDriver::VirtualMachine::Disk.one_disk(disk_id, one_disk)
    new_size = disk.new_size

    # TODO: we should think about what to do with swap_disks
    is_storage_drs = target_ds_ref.start_with?('group-')

    if !(!disk.managed? || (is_storage_drs && disk.volatile?))
        if is_storage_drs
            # Get a single DS using StoragePod recommendation
            # We overwrite the reference in target_ds_ref from the StoragePod
            # to the single recommended DS
            target_ds_ref = vm.recommended_ds(target_ds_ref)
        end

        source_ds_vc = VCenterDriver::Datastore.new_from_ref(source_ds_ref,
                                                             vi_client)

        if source_ds_ref == target_ds_ref
            target_ds_vc = source_ds_vc
        else
            target_ds_vc = VCenterDriver::Storage.new_from_ref(target_ds_ref,
                                                               vi_client)
        end

        target_ds_name_vc = target_ds_vc['name']

        source_ds_vc.copy_virtual_disk(src_path, target_ds_vc,
                                       target_path, new_size)
    end
rescue StandardError => e
    message = "Error clone virtual disk #{src_path} in "\
              "datastore #{target_ds_name_vc}. "\
              "Reason: #{e.message}." \
              "on attempt #{retries}."

    if op_retry && (retries += 1) > CONFIG[:retries]
        message << "\nPlease check vCenter logs directly\n"
    end

    OpenNebula.log_error(message)
    if VCenterDriver::CONFIG[:debug_information]
        STDERR.puts "#{message} #{e.backtrace}"
    end

    sleep 60 if op_retry
    retry if op_retry && retries < CONFIG[:retries]

    exit(-1)
ensure
    vi_client.close_connection if vi_client
end
